﻿using AutoMapper;
using HuiLian.Common.HdlHttpModel;
using HuiLian.Common.Helpers;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform.HdlScadaAlarmRealTime;
using HuiLian.Service.HdlScada.AlarmRealTime.Output;
using HuiLian.Service.HdlScada.HdlScadaAlarmHistory.Input;
using HuiLian.Service.Platform.CallTime.Output;
using Microsoft.AspNetCore.Mvc;
using NPOI.POIFS.Crypt.Dsig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuiLian.Service.HdlScada.AlarmRealTime
{
    /// <summary>
    /// 实时报警记录
    /// </summary>
    public class HdlScadaAlarmRealTimeService : IHdlScadaAlarmRealTimeService
    {
        private readonly IMapper _mapper;
        private readonly IHdlScadaAlarmRealTimeRepository _hdlScadaAlarmRealTimeRepository;
        private readonly HttpHelper _httpHelper;

        public HdlScadaAlarmRealTimeService(IMapper mapper, IHdlScadaAlarmRealTimeRepository hdlScadaAlarmRealTimeRepository, HttpHelper httpHelper)
        {
            _mapper = mapper;
            _hdlScadaAlarmRealTimeRepository = hdlScadaAlarmRealTimeRepository;
            _httpHelper = httpHelper;
        }

        /// <summary>
        /// 获取实时报警记录
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _hdlScadaAlarmRealTimeRepository.Select.Where(p => p._id == id.ToString()).IncludeMany(p => p.Anls).IncludeMany(p => p.Acrs).FirstAsync();
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询调用时间表信息
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<HdlScadaAlarmRealTimeEntity> input)
        {
            //var key = input.Filter?.Label;

            var list = await _hdlScadaAlarmRealTimeRepository.Select
            .WhereDynamicFilter(input.DynamicFilter)
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
            .Count(out var total)
            .OrderByDescending(true, c => c.stime)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync(p => new HdlScadaAlarmRealTimeListOutput { stime = p.stime });

            var data = new PageOutput<HdlScadaAlarmRealTimeListOutput>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }

        /// <summary>
        /// 查询实时报警记录列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetListAsync()
        {
            var result = await _hdlScadaAlarmRealTimeRepository.Select.ToListAsync();
            return ResponseOutput.Ok(result);
        }

        public async Task<IResponseOutput> HdlScadaAlarmRealtimeAsync()
        {
            HdlScadaAlarmRealtimeRequest request = new HdlScadaAlarmRealtimeRequest();
            var data = await _httpHelper.HdlScadaAlarmRealtimeAsync(request);

            //var realTime = System.IO.File.ReadAllText(@"C:\Users\admin\Desktop\realTime.json");
            //data = Newtonsoft.Json.JsonConvert.DeserializeObject<HdlScadaAlarmRealtimeResponse>(realTime);

            if (data != null && data.Response != null && data.Response.Count > 0)
            {
                var entity = _mapper.Map<List<HdlScadaAlarmRealtimeOutput>>(data.Response);
                foreach (var item in entity)
                {
                    if (!string.IsNullOrWhiteSpace(item.station_position))
                    {
                        string[] station_position = item.station_position.Split(",");
                        if(station_position.Length == 2)//x,y是标准坐标格式
                        {
                            item.station_position = $"4{station_position[0]},38{station_position[1]}";
                        }
                    }
                    
                }
                return ResponseOutput.Ok(entity);
            }
            else
            {
                return ResponseOutput.NotOk("没有获取到实时报警记录");
            }            
        }

    }
}
